package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApForwardBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApForwardBehaviorService;
import com.heima.common.exception.AppHttpCodeEnum;
import com.heima.common.exception.LeadNewsException;
import com.heima.model.behavior.dtos.FollowBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApFollowBehavior;
import com.heima.model.behavior.pojos.ApForwardBehavior;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * APP转发行为表 服务实现类
 * </p>
 *
 * @author heima
 * @since 2021-08-16
 */
@Service
@Transactional
public class ApForwardBehaviorServiceImpl extends ServiceImpl<ApForwardBehaviorMapper, ApForwardBehavior> implements ApForwardBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Override
    public void saveFollowBehavior(FollowBehaviorDto dto) {

        ApBehaviorEntry behaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(dto.getUserId(), null);
        if (behaviorEntry == null) {
            return;
        }
        try {
            ApFollowBehavior followBehavior = new ApFollowBehavior();
            followBehavior.setEntryId(behaviorEntry.getId());
            followBehavior.setArticleId(dto.getArticleId());
            followBehavior.setFollowId(dto.getFollowId());
            followBehavior.setCreatedTime(new Date());
        } catch (Exception e) {
            e.printStackTrace();
            throw new LeadNewsException(AppHttpCodeEnum.SERVER_ERROR);
        }

    }

}
